package com.zshy.base.job.autoconfig;

import com.xxl.job.core.executor.impl.XxlJobSpringExecutor;
import com.zshy.base.job.properties.XxlJobProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * xxl-job自动配置
 *
 * @author zhouhengzhe
 */
@Configuration
@EnableConfigurationProperties({XxlJobProperties.class})
@Slf4j
public class XxlJobAutoConfiguration {

    @Bean(initMethod = "start", destroyMethod = "destroy")
    public XxlJobSpringExecutor xxlJobSpringExecutor(XxlJobProperties prop) {
        log.info(">>>>>>>>>>>star xxl job config init...");
        XxlJobSpringExecutor xxlJobSpringExecutor = new XxlJobSpringExecutor();
        xxlJobSpringExecutor.setAdminAddresses(prop.getAdmin().getAdminAddresses());
        xxlJobSpringExecutor.setAppname(prop.getExecutor().getAppName());
        xxlJobSpringExecutor.setIp(prop.getExecutor().getIp());
        xxlJobSpringExecutor.setPort(prop.getExecutor().getPort());
        xxlJobSpringExecutor.setAccessToken(prop.getExecutor().getAccessToken());
        xxlJobSpringExecutor.setLogPath(prop.getExecutor().getLogPath());
        xxlJobSpringExecutor.setLogRetentionDays(prop.getExecutor().getLogRetentionDays());
        return xxlJobSpringExecutor;
    }
}
